package com.dp.mallchat.common.user.service.adapter;

import cn.hutool.core.bean.BeanUtil;
import com.dp.mallchat.common.chat.domain.vo.resp.ChatMemberStatisticResp;
import com.dp.mallchat.common.chat.domain.vo.resp.ChatMessageResp;
import com.dp.mallchat.common.chat.domain.vo.resp.ReCallResp;
import com.dp.mallchat.common.chat.service.ChatService;
import com.dp.mallchat.common.common.domain.dto.MsgMarkEventDto;
import com.dp.mallchat.common.common.domain.enums.YesOrNoEnum;
import com.dp.mallchat.common.user.domain.ws.enums.ChatActiveStatusEnum;
import com.dp.mallchat.common.user.domain.ws.enums.WSRespTypeEnum;
import com.dp.mallchat.common.user.domain.ws.resp.*;
import com.dp.mallchat.common.user.domain.user.entity.User;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * @Author: dupeng
 * @CreateTime: 2024-05-22  18:50
 * @Description: TODO
 */
@Component
public class WSAdapter {

    @Autowired
    private ChatService chatService;

    /**
     * 组装请求二维码响应
     *
     * @param wxMpQrCodeTicket
     * @return
     */
    public static WSBaseResp<WSLoginUrl> buildLoginResp(WxMpQrCodeTicket wxMpQrCodeTicket) {
        WSBaseResp<WSLoginUrl> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_URL.getType());
        WSLoginUrl wsLoginUrl = new WSLoginUrl();
        wsLoginUrl.setLoginUrl(wxMpQrCodeTicket.getUrl());
        wsBaseResp.setData(wsLoginUrl);
        return wsBaseResp;
    }

    /**
     * 组装认证授权信息
     *
     * @param user
     * @param token
     * @param isPower
     * @return
     */
    public static WSBaseResp<?> buildResp(User user, String token, boolean isPower) {
        WSBaseResp<WSLoginSuccess> wsBaseResp = new WSBaseResp<>();
        WSLoginSuccess wsLoginSuccess = WSLoginSuccess.builder()
                .uid(user.getId())
                .avatar(user.getAvatar())
                .name(user.getName())
                .token(token)
                .power(isPower ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus())
                .build();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());
        wsBaseResp.setData(wsLoginSuccess);
        return wsBaseResp;
    }

    /**
     * 扫码等待授权
     *
     * @return
     */
    public static WSBaseResp<?> buildWaitAuthorizationResp() {
        WSBaseResp<WSLoginUrl> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildAuthorizationFailResp() {
        WSBaseResp<WSLoginUrl> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildPushMsg(ChatMessageResp chatMessageResp) {
        WSBaseResp<ChatMessageResp> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.MESSAGE.getType());
        wsBaseResp.setData(chatMessageResp);
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildReCallMsg(ReCallResp reCallResp) {
        WSBaseResp<WSMsgRecall> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.RECALL.getType());
        WSMsgRecall wsMsgRecall = new WSMsgRecall();
        BeanUtils.copyProperties(reCallResp, wsMsgRecall);
        resp.setData(wsMsgRecall);
        return resp;
    }

    public static WSBaseResp<?> buildMsgMark(MsgMarkEventDto dto, Integer msgMarkCount) {
        WSMsgMark.WSMsgMarkItem item = new WSMsgMark.WSMsgMarkItem();
        BeanUtils.copyProperties(dto, item);
        item.setMarkCount(msgMarkCount);
        WSBaseResp<WSMsgMark> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.MARK.getType());
        WSMsgMark mark = new WSMsgMark();
        mark.setMarkList(Collections.singletonList(item));
        wsBaseResp.setData(mark);
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildApplySend(WSFriendApply resp) {
        WSBaseResp<WSFriendApply> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.APPLY.getType());
        wsBaseResp.setData(resp);
        return wsBaseResp;
    }

    public WSBaseResp<WSOnlineOfflineNotify> buildOfflineNotifyResp(User user) {
        WSBaseResp<WSOnlineOfflineNotify> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.ONLINE_OFFLINE_NOTIFY.getType());
        WSOnlineOfflineNotify onlineOfflineNotify = new WSOnlineOfflineNotify();
        onlineOfflineNotify.setChangeList(Collections.singletonList(buildOfflineInfo(user)));
        assembleNum(onlineOfflineNotify);
        wsBaseResp.setData(onlineOfflineNotify);
        return wsBaseResp;
    }


    private static ChatMemberResp buildOfflineInfo(User user) {
        ChatMemberResp info = new ChatMemberResp();
        BeanUtil.copyProperties(user, info);
        info.setUid(user.getId());
        info.setActiveStatus(ChatActiveStatusEnum.OFFLINE.getStatus());
        info.setLastOptTime(user.getLastOptTime());
        return info;
    }


    private void assembleNum(WSOnlineOfflineNotify onlineOfflineNotify) {
        ChatMemberStatisticResp memberStatistic = chatService.getMemberStatistic();
        onlineOfflineNotify.setOnlineNum(memberStatistic.getOnlineNum());
    }

    public WSBaseResp<WSOnlineOfflineNotify> buildOnlineNotifyResp(User user) {
        WSBaseResp<WSOnlineOfflineNotify> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.ONLINE_OFFLINE_NOTIFY.getType());
        WSOnlineOfflineNotify onlineOfflineNotify = new WSOnlineOfflineNotify();
        onlineOfflineNotify.setChangeList(Collections.singletonList(buildOnlineInfo(user)));
        assembleNum(onlineOfflineNotify);
        wsBaseResp.setData(onlineOfflineNotify);
        return wsBaseResp;
    }

    private static ChatMemberResp buildOnlineInfo(User user) {
        ChatMemberResp info = new ChatMemberResp();
        BeanUtil.copyProperties(user, info);
        info.setUid(user.getId());
        info.setActiveStatus(ChatActiveStatusEnum.ONLINE.getStatus());
        info.setLastOptTime(user.getLastOptTime());
        return info;
    }
}
